package com.hxf.service.role.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.common.PublicUtil;
import com.hxf.dto.MenuDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.RoleDto;
import com.hxf.dto.RoleMenuDto;
import com.hxf.dto.UserDto;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.service.base.impl.BaseServiceImpl;
import com.hxf.service.role.RoleService;
import com.hxf.web.vo.RoleMenuVo;

/**
 * DAO权限业务类
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository("roleService")
public class RoleServiceImpl extends BaseServiceImpl implements RoleService{

	@Override
	public ReturnData<RoleDto> findByRole(RoleDto roleDto, int targetPage, int pageSize) throws ServiceException {
		try {
			//List<RoleDto> selectRoleList = super.daoFacade.getUserDao().findByUserRole(urDto);
			//List<RoleDto> selectRoleList = super.daoFacade.getUserDao().findByRole(roleDto, targetPage, pageSize);
			return super.daoFacade.getRoleDao().findByRole(roleDto, targetPage, pageSize);
			
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		} 
	}
	
	@Override
	public ReturnData<RoleDto> findByRoleOrAdmin(RoleDto roleDto,int targetPage,int pageSize) throws ServiceException, DaoLevelException{
		UserDto userDto = super.daoFacade.getUserDao().selectUserOrAdmin(roleDto.getRoleId());
		//超级管理员  显示所有权限
		//null !=userDto && (null == userDto.getParentId()||"null".equals(userDto.getParentId()))
		//  && (null==userDto.getParentUserId() || "null".equals(userDto.getParentUserId()))
		if(null != userDto && (null==userDto.getParentId()||"null".equals(userDto.getParentId())||
				(null==userDto.getParentUserId() || "null".equals(userDto.getParentUserId())))){
			
			return super.daoFacade.getRoleDao().findByRole(roleDto, targetPage, pageSize);
		}else{
			//根据登陆用户，显示该用户所拥有的权限
			return super.daoFacade.getRoleDao().findByUserRole(roleDto, targetPage, pageSize);
		}
	}
	
	@Override
	public List<RoleDto> findByRole(RoleDto roleDto) throws ServiceException {
		try {
			return super.daoFacade.getRoleDao().findByRole(roleDto);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		} 
	}

	@Override
	public List<RoleMenuVo> findByRoleAddMenu(RoleMenuDto roleMenuDto) throws ServiceException { 
		try {
			//查询所有菜单
			List<MenuDto> menuList = this.daoFacade.getMenuDao().findByMenu(null);
			//查询已分配给当前用户的菜单
			List<MenuDto> selectMenuList = this.daoFacade.getRoleDao().findByRoleMenu(roleMenuDto);
			  MenuDto rd = null;
			  MenuDto srd = null;
			  RoleMenuVo rmVo = null;
			  List<RoleMenuVo> rmList = new ArrayList<RoleMenuVo>();
			  if (menuList != null && menuList.size() > 0) {
				  for (int i = 0;i < menuList.size(); i++) {
					  rd = menuList.get(i);
					  rmVo = new RoleMenuVo();
					  rmVo.setId(rd.getMenuId());
					  rmVo.setName(rd.getMenuName()); 
					  rmVo.setpId(rd.getParentId());
					  rmVo.setChecked(false); 
					  //已分票的菜单为true
					  if (selectMenuList != null && selectMenuList.size() > 0) {
						  for (int j = 0;j < selectMenuList.size(); j++) {
							  srd = selectMenuList.get(j);
							  if (rd.getMenuId() == srd.getMenuId()) {
								  rmVo.setChecked(true); 
							  }
						  }
					  }
					  rmList.add(rmVo); 
				  }
			  }
			  return rmList;
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		} 
	}

	@Override
	public void saveRoleAddMenu(RoleDto roleDto, String menuIds) throws ServiceException { 
		try {
			//删除当前角色绑定菜单
			super.daoFacade.getRoleDao().deleteRoleMenuByRoleId(roleDto.getRoleId());
			
			RoleMenuDto urDto = null;
			if (PublicUtil.isNotNullAndEmpty(menuIds)) {
				String [] ids = menuIds.split(",");
				for (int i = 0;i < ids.length; i++) {
					urDto = new RoleMenuDto();
					urDto.setRoleId(roleDto.getRoleId());
					urDto.setMenuId(Integer.parseInt(ids[i])); 
					super.daoFacade.getRoleDao().create(urDto);
				}
			}
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
}
